package work.lrl.customer_unbinding;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.SneakyThrows;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Gong.Yang
 */
public class DemoJsonArrToCsv {
    static final String path = "C:\\work-records\\l陆叶\\le-会员解绑\\2023-08-30Out\\";

    public static void main(String[] args) {
        List<File> files = FileUtil.loopFiles(path);
        for (File file : files) {
            jsonArrToExcel(file);
        }
    }

    @SneakyThrows
    static void jsonArrToCsv(File jsonFile) {
        String content = FileUtil.readString(jsonFile, Charset.defaultCharset());
        String csvFIleName = path + jsonFile.getName().split("\\.")[0] + ".csv";
        FileWriter fileWriter = new FileWriter(csvFIleName);
        String item = "'%s";

        if (content.startsWith("[")) {
            JSONArray array = JSONUtil.parseArray(content);
            for (Object o : array) {
                if (o instanceof String s) {
                    fileWriter.write(String.format(item, s) + "\n");
                }
            }
        } else if (content.startsWith("{")) {
            JSONObject entries = JSONUtil.parseObj(content);
            for (Map.Entry<String, Object> entry : entries) {
                String s = String.format(item, entry.getKey()) + ","
                        + String.format(item, entry.getValue()) + "\n";
                fileWriter.write(s);
            }
        }
        fileWriter.close();
    }

    @SneakyThrows
    static void jsonArrToExcel(File jsonFile) {
        String content = FileUtil.readString(jsonFile, Charset.defaultCharset());
        String csvFIleName = path + jsonFile.getName().split("\\.")[0] + ".xlsx";

        if (content.startsWith("[")) {
            JSONArray array = JSONUtil.parseArray(content);
            List<UserInfo> list = new ArrayList<>();
            for (Object o : array) {
                if (o instanceof String s) {
                    list.add(new UserInfo(s));
                }
            }
            EasyExcel.write(csvFIleName).sheet("sheet1").doWrite(list);
        } else if (content.startsWith("{")) {
            JSONObject entries = JSONUtil.parseObj(content);
            List<UserMap> userMaps = new ArrayList<>();
            for (Map.Entry<String, Object> entry : entries) {
                UserMap userMap = new UserMap(entry.getKey(), (String) entry.getValue());
                userMaps.add(userMap);
            }
            EasyExcel.write(csvFIleName).sheet("sheet1").doWrite(userMaps);
        }
    }


    @Data
    @AllArgsConstructor
    static private class UserInfo {
        @ExcelProperty("用户信息")
        private String userInfo;
    }


    @Data
    @AllArgsConstructor
    static private class UserMap {
        @ExcelProperty("会员ID（我方）")
        private String customerId;
        @ExcelProperty("会员卡号（三方）")
        private String thridCode;
    }
}
